<?php
if (!isset($model))
    $model = new BDynamicFormModel ();

$ProductIdField = 'media_productId_priceConfig_product';
$productCategoryField = 'media_productCategory_priceConfig_product';
$model->addField($ProductIdField);
$model->addField($productCategoryField);

$priceConfigProductView = array();

if (isset($orderPriceConfigId) && $orderPriceConfigId > 0) {
    $priceConfigTable = new BPSheet('priceConfigProduct');
    $priceConfig = $priceConfigTable->getListById($orderPriceConfigId);

    $priceConfigProductTable = new BPSheet('priceConfigProduct', $priceConfig);
    $priceConfigProductView = $priceConfigProductTable->getView();
}
?>
<?php
$app = Blyn::app()->getActiveApp();
?>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "addPriceConfigForm",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
$url = $control->getAjaxLinkUrl('savePriceConfig', NULL, array(
    'eventId' => $eventId,
    'orderPriceConfigId' => $orderPriceConfigId));
$updateId = $control->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function saveOrderPriceProduct() {

        var formData = new FormData($("#addPriceConfigForm")[0]);
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            //datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(html) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(html);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>

<div class="well-small row-fluid">
    <div class="input-prepend span4">
        <span class="add-on">产品ID</span>
        <?php
        echo CHtml::activeTextField($model, $ProductIdField, array('placeholder' => 'productId' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="span1"> -or-</div>
    <div class="input-prepend span4">
        <span class="add-on">产品分类</span>
        <?php
        echo CHtml::activeTextField($model, $productCategoryField, array('placeholder' => 'productCategory' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    
    <div class="input-append span2">
        <?php
        echo $control->getAjaxLink(array('name' => '添加产品', 'id' => 'addPriceConfigProduct'), array(
            'htmlOptions' => array('class' => 'btn'),
            'orderPriceConfigId' => $orderPriceConfigId,
            'eventId' => $eventId,
        ));
        ?>
    </div>
</div>
<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span4">
        <col class="span4">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo '类型'; ?></th>
            <th><?php echo 'ID'; ?></th>
            <th><?php echo '操作'; ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        if ($orderPriceConfigId > 0) {
            foreach ($priceConfigProductView as $priceConfigProduct) {
                $productConfigId = $priceConfigProduct->getId();
                ?>
                <tr>
                    <?php
                    $productIdItem = $priceConfigProduct->getItem('productId');
                    $productCategoryItem = $priceConfigProduct->getItem('productCategory');
                    $productId = '';
                    $categoryId = '';
                    $type = '';
                    if ($productIdItem instanceof BTreeItem) {
                        $productId = $productIdItem->getValue();
                        $type = 'ProductId';
                        ?>
                        <td> <?php echo $type; ?>  </td>   
                        <td> <?php echo $productId; ?>  </td>   
                    <?php } ?>
                    <?php
                    if ($productCategoryItem instanceof BTreeItem) {
                        $productCategory = $productCategoryItem->getValue();
                        $type = 'ProductCategory';
                        ?>
                        <td> <?php echo $type; ?>  </td>   
                        <td> <?php echo $productCategory; ?>  </td>   
                    <?php } ?>
                    <td>
                        <?php
                        echo $control->getAjaxLink(array('name' => '删除', 'id' => 'deletePriceConfigProduct'), array(
                            'htmlOptions' => array('class' => 'btn'),
                            'orderPriceConfigId' => $orderPriceConfigId,
                            'productConfigId' => $productConfigId
                        ));
                        ?>
                    </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table>
<?php
$this->endWidget();
//echo CHtml::endForm();
?>
